package com.dpf.hou.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dpf.hou.mapper.AdoptionMapper;
import com.dpf.hou.mapper.PetMapper;
import com.dpf.hou.pojo.Adoption;
import com.dpf.hou.pojo.Pet;
import com.dpf.hou.pojo.User;
import com.dpf.hou.service.PetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PetServiceImp implements PetService {
    @Autowired
    PetMapper petMapper;
    @Override
    public List<Pet> getAllPet() {
        List<Pet> list=petMapper.selectList(null);
        return list;
    }

    @Override
    public boolean updatePet(Pet pet) {
        QueryWrapper<Pet> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("petid",pet.getPetid());
        int rows=petMapper.update(pet,queryWrapper);
        return rows == 0? false:true;
    }

    @Override
    public boolean addPet(Pet pet) {
        QueryWrapper<Pet> q=new QueryWrapper<>();
        int rows=petMapper.insert(pet);
        return rows == 0 ? false:true;
    }

    @Override
    public Page<Pet> getPage(int page, int size) {
        Page<Pet> p=new Page<>(page,size);
        return petMapper.selectPage(p,null);
    }

    @Override
    public Page<Pet> getPageByName(int page, int size, String petname) {
        QueryWrapper<Pet> q=new QueryWrapper<>();
        if(petname!=null && !"".equals(petname)){
            q.like("petname",petname);
        }
        Page<Pet> p=new Page<>(page,size);
        return petMapper.selectPage(p,q);
    }

    @Override
    public boolean delBatch(List<Integer> petid) {
        int n=petid.size();
        if(n==0){
            return false;
        }
        for (int i=0;i<n;i++){
            petMapper.deleteById(petid.get(i));
        }
        return true;
    }

    @Override
    public List<Pet> list() {
        return petMapper.selectList(null);
    }

    @Override
    public void saveBatch(List<Pet> list) {
        for (int i=0;i<list.size();i++){
            addPet(list.get(i));
        }
    }

    @Override
    public Page<Pet> petGet(int page, int size, String petname) {
        QueryWrapper<Pet> q=new QueryWrapper<>();
        if(petname!=null && !"".equals(petname)){
            q.like("petname",petname);
        }
        q.eq("status",0);
        Page<Pet> p=new Page<>(page,size);
        return petMapper.selectPage(p,q);
    }

    @Override
    public boolean updateById(Pet pet) {
        int status=pet.getStatus();
        QueryWrapper<Pet> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("petid",pet.getPetid());
        pet=petMapper.selectOne(queryWrapper);
        pet.setStatus(status);
        int rows=petMapper.updateById(pet);
        return rows==0?false:true;
    }
}
